package org.mozhu.mboot.core.web.interceptor;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class PjaxInterceptor extends BaseHandlerInterceptorAdapter {

	public static final String _PJAX = "_pjax";
	public static final String PJAX = "pjax";
	public static final String X_PJAX = "X-PJAX";

	@Override
	protected boolean doPreHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
		return true;
	}

	@Override
	public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
		String header = request.getHeader(X_PJAX);
		if (StringUtils.isNotBlank(header) && modelAndView!=null) {
			modelAndView.addObject("decorator", PJAX);
			request.setAttribute("decorator", PJAX);
		}
	}
}